package com.yumijinfu.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.entity.HongBaoInfo;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.pullrefreshlistview.XListView;
import com.yumijinfu.response.HongBaoRes;
import com.yumijinfu.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的红包
 */
public class MyHongBaoActivity extends BaseActivity implements XListView.IXListViewListener {
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.tv_tab_1)
    TextView tvTab1;
    @Bind(R.id.tv_tab_2)
    TextView tvTab2;
    @Bind(R.id.tv_tab_3)
    TextView tvTab3;
    @Bind(R.id.lly_tab)
    LinearLayout llyTab;
    @Bind(R.id.listview_tab1)
    XListView listviewTab1;

    //
    private  TabAdater tabAdater;
    private  int  queryType=0;   //0表示未使用，1表示已使用，2表示已过期
    private  int  pageNum=1;
    private boolean falg=true;
    //
    private  List<HongBaoInfo> pages=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_hong_bao);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titlebar.setTitleBar(R.drawable.back_selector, "我的红包", 0);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void setData() {
        tabAdater = new TabAdater(MyHongBaoActivity.this, pages,queryType);
        listviewTab1.setPullLoadEnable(true);
        listviewTab1.setXListViewListener(this);
        listviewTab1.setAdapter(tabAdater);
        //
        getdata(queryType,pageNum);

    }

    /**
     *
     *   获取红包数据
     * @param queryType
     * @param pageNum
     */

    private void getdata(final  int queryType, int pageNum) {
        HttpParams params=new HttpParams();
        params.put("queryType",queryType+"");
        params.put("pageNum",pageNum+"");
        HttpUtils.post(Config.PREFERENTIALRED, params, new HttpCallBack(this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if(null!=result){
                    try {
                        HongBaoRes res=new HongBaoRes(new JSONObject(result.toString()));
                        if(falg==true){
                            if(res.getPage().size()>0){
                                pages.clear();
                                pages = res.getPage();
                            }else {
                                pages.clear();
                                Toast.makeText(MyHongBaoActivity.this, "无数据", Toast.LENGTH_SHORT).show();
                            }

                        }else{
                            if(!res.getCurrentPage().equals(res.getTotalPage())){
                                pages.addAll(res.getPage());
                            }else{
                                if(res.getPage().size()>0){  //相等表示并且getPage().size()>0表示最后一页
                                    pages.addAll(res.getPage());
                                }else{
                                    Toast.makeText(MyHongBaoActivity.this, "无更多加载数据", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                        //
                        tabAdater.UpdateAdapter(pages,queryType);
                        //释放
                        onLoad();

                    } catch (JSONException e) {

                    }

                }


                onLoad();
            }

            @Override
            public void onError(String code, String message) {

               onLoad();
                Toast.makeText(MyHongBaoActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }

    @OnClick({R.id.titlebar_iv_back,R.id.tv_tab_1, R.id.tv_tab_2, R.id.tv_tab_3})
    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_iv_back:
                finish();

                break;

            case  R.id.tv_tab_1:
                queryType=0;
                pageNum=1;
                falg=true;
                pages.clear();
                getdata(queryType, pageNum);
                ResetTextColor();
                listviewTab1.setVisibility(View.VISIBLE);
                llyTab.setBackgroundResource(R.mipmap.tab_1);
                tvTab1.setTextColor(getResources().getColor(R.color.white));

                break;

            case  R.id.tv_tab_2:
                queryType=1;
                pageNum=1;
                falg=true;
                pages.clear();
                getdata(queryType, pageNum);
                ResetTextColor();
                llyTab.setBackgroundResource(R.mipmap.tab_2);
                tvTab2.setTextColor(getResources().getColor(R.color.white));
                break;


            case  R.id.tv_tab_3:
                queryType=2;
                pageNum=1;
                falg=true;
                pages.clear();
                getdata(queryType, pageNum);
                ResetTextColor();
                tvTab3.setTextColor(getResources().getColor(R.color.white));
                llyTab.setBackgroundResource(R.mipmap.tab_3);
                break;

            default:
        }

    }

    /**
     * 重置tab颜色字体
     */

    public void ResetTextColor() {
        tvTab1.setTextColor(getResources().getColor(R.color.black_6));
        tvTab2.setTextColor(getResources().getColor(R.color.black_6));
        tvTab3.setTextColor(getResources().getColor(R.color.black_6));
    }

    @Override
    public void onRefresh() {
        falg =true;
        pageNum=1;
        getdata(queryType,pageNum);
    }

    @Override
    public void onLoadMore() {
        falg =false;
        pageNum++;
        getdata(queryType,pageNum);
    }




    class TabAdater extends BaseAdapter{
        private Context context;
        private List<HongBaoInfo> infos;
        private LayoutInflater layoutInflater;
        private  int type;

        public  void UpdateAdapter(List<HongBaoInfo> infos,int  queryType){
            this.infos=infos;
            this.type=queryType;
            notifyDataSetChanged();
        }

        public TabAdater(Context context, List<HongBaoInfo> infos){
            this.context=context;
            this.infos=infos;
             layoutInflater = LayoutInflater.from(context);
        }

        public TabAdater(Context context, List<HongBaoInfo> infos,int type){
            this.context=context;
            this.infos=infos;
            layoutInflater = LayoutInflater.from(context);
            this.type=type;
        }

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder  viewHolder=null;
            if(viewHolder==null){
                viewHolder=new ViewHolder();
                convertView= layoutInflater.inflate(R.layout.hongbao_tab_item,null);
                viewHolder.ll_hongbao_bg= (LinearLayout) convertView.findViewById(R.id.ll_hongbao_bg);
                viewHolder.tv_money= (TextView) convertView.findViewById(R.id.tv_money);
                viewHolder.tv_type= (TextView) convertView.findViewById(R.id.tv_type);
                viewHolder.tv_validTime= (TextView) convertView.findViewById(R.id.tv_validTime);
                viewHolder.tv_description= (TextView) convertView.findViewById(R.id.tv_description);
                //
                viewHolder.tv_txt1= (TextView) convertView.findViewById(R.id.tv_txt1);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }

            HongBaoInfo info=infos.get(position);

            if(type==0){
                viewHolder.ll_hongbao_bg.setBackgroundResource(R.mipmap.hongbao_tab1);
                viewHolder.tv_money.setTextColor(getResources().getColor(R.color.dan_hong_3));
                viewHolder.tv_type.setText("可用红包");
            }else if(type==1){
                viewHolder.ll_hongbao_bg.setBackgroundResource(R.mipmap.hongbao_tab2_bg);
                viewHolder.tv_money.setTextColor(getResources().getColor(R.color.dan_hong_3));
                viewHolder.tv_type.setText("已用红包");

            }else {
                viewHolder.ll_hongbao_bg.setBackgroundResource(R.mipmap.hongbao_tab2_bg);
                viewHolder.tv_money.setTextColor(getResources().getColor(R.color.black_8));
                viewHolder.tv_type.setText("过期红包");
            }

            if(null!=info.getRedMoney()){
              String money= info.getRedMoney().substring(0,info.getRedMoney().lastIndexOf("."));
                viewHolder.tv_money.setText(money);
            }

            viewHolder.tv_validTime.setText("有效期至:"+info.getValidTime());

            viewHolder.tv_description.setText("最低消费:"+info.getMinMoney()+"元");
            viewHolder.tv_txt1.setText(info.getActivityName());

            return convertView;
        }
    }

    public  static class  ViewHolder{
        LinearLayout ll_hongbao_bg;
        TextView tv_money;
        TextView tv_type;
        TextView tv_validTime;
        TextView tv_description;
        TextView tv_txt1;

    }

    private void onLoad() {
        listviewTab1.stopRefresh();
        listviewTab1.stopLoadMore();
        listviewTab1.setRefreshTime("刚刚");
    }
}
